package com.zadetk.helpyou.activities;

import android.Manifest;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.zadetk.helpyou.R;
import com.zadetk.helpyou.base.AbsActivity;
import com.zadetk.helpyou.bean.InfoItemBean;
import com.zadetk.helpyou.net.BaseObserver;
import com.zadetk.helpyou.net.BaseResponse;
import com.zadetk.helpyou.net.NetTool;
import com.zadetk.helpyou.other.Const;
import com.zadetk.helpyou.other.UserInfoManger;
import com.zadetk.helpyou.utils.LogUtil;
import com.zadetk.helpyou.utils.ToastUtil;

import java.io.File;
import java.util.ArrayList;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;
import me.nereo.multi_image_selector.MultiImageSelector;
import okhttp3.MultipartBody;

/**
 * 4-2-1证件上传页面
 */
public class UpLoadCredentialActivity extends AbsActivity {

    public static final int TypePerson = 1;
    public static final int TypeCompaney = 2;
    public static final String Type = "type";
    private int type;
    private ImageView iv1, iv2;

    @Override
    public int setView() {
        return R.layout.activity_up_load_credential;
    }

    @Override
    public void initBasic(Bundle savedInstanceState) {
        type = getIntent().getIntExtra(Type, -1);
        initViews();
        initLogic();
    }

    private void initLogic() {
        iv1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                performCodeWithPermission("需要相册权限！", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).single().start(UpLoadCredentialActivity.this, 300);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
            }
        });
        iv2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                performCodeWithPermission("需要相册权限！", new PermissionCallback() {
                    @Override
                    public void hasPermission() {
                        MultiImageSelector.create().showCamera(true).single().start(UpLoadCredentialActivity.this, 301);
                    }

                    @Override
                    public void noPermission() {

                    }
                }, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE, Manifest.permission.CAMERA});
            }
        });
    }

    private void initViews() {
        setTopTitle("上传证件");
        iv1 = findViewById(R.id.iv_1);
        iv2 = findViewById(R.id.iv_2);
        if (type == TypeCompaney) {
            Glide.with(this)
                    .load(UserInfoManger.userInfo.getCompanyidcard_front())
                    .placeholder(R.drawable.companey_front)
                    .error(R.drawable.companey_front)
                    .into(iv1);
            Glide.with(this)
                    .load(UserInfoManger.userInfo.getCompanyidcard_back())
                    .placeholder(R.drawable.companey_back)
                    .error(R.drawable.companey_back)
                    .into(iv2);
        } else {
            Glide.with(this)
                    .load(UserInfoManger.userInfo.getId_up())
                    .placeholder(R.drawable.id_up)
                    .error(R.drawable.id_up)
                    .into(iv1);
            Glide.with(this)
                    .load(UserInfoManger.userInfo.getId_down())
                    .placeholder(R.drawable.id_down)
                    .error(R.drawable.id_down)
                    .into(iv2);
        }
    }

    private void changeInfoRequest(String key, File file) {
        MultipartBody multipartBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addPart(NetTool.buildUploadMultipart(file, "value"))
                .addFormDataPart("key", key)
                .build();
        NetTool.getApi().saveUserInfo(multipartBody)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<BaseResponse<InfoItemBean>>() {
                    @Override
                    public void onData(BaseResponse<InfoItemBean> value) {
                        if (value.getCode() == 0) {
                            switch (value.getData().getKey()) {
                                case Const.TYPE_COMPANEY_IMG:
                                    Glide.with(UpLoadCredentialActivity.this).load(value.getData().getValue()).into(iv1);
                                    UserInfoManger.userInfo.setCompanyidcard_front(value.getData().getValue());
                                    break;
                                case Const.TYPE_COMPANEY_LICENCE:
                                    Glide.with(UpLoadCredentialActivity.this).load(value.getData().getValue()).into(iv2);
                                    UserInfoManger.userInfo.setCompanyidcard_back(value.getData().getValue());
                                    break;
                                case Const.TYPE_ID_UP:
                                    Glide.with(UpLoadCredentialActivity.this).load(value.getData().getValue()).into(iv1);
                                    UserInfoManger.userInfo.setId_up(value.getData().getValue());
                                    break;
                                case Const.TYPE_ID_down:
                                    Glide.with(UpLoadCredentialActivity.this).load(value.getData().getValue()).into(iv2);
                                    UserInfoManger.userInfo.setId_down(value.getData().getValue());
                                    break;
                            }
                            ToastUtil.showToast("修改成功！");
                        } else {
                            ToastUtil.showToast(value.getMessage());
                        }
                    }
                });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            ArrayList<String> list = data.getStringArrayListExtra(MultiImageSelector.EXTRA_RESULT);
            LogUtil.d(list.get(0));
            ToastUtil.showToast(list.get(0));
            File file = new File(list.get(0));

            if (requestCode == 300 && type == TypeCompaney) {
                changeInfoRequest(Const.TYPE_COMPANEY_IMG, file);
            } else if (requestCode == 301 && type == TypeCompaney) {
                changeInfoRequest(Const.TYPE_COMPANEY_LICENCE, file);
            } else if (requestCode == 300 && type == TypePerson) {
                changeInfoRequest(Const.TYPE_ID_UP, file);
            } else if (requestCode == 301 && type == TypePerson) {
                changeInfoRequest(Const.TYPE_ID_down, file);
            }

        }
    }
}
